package com.DHH.Service.Api;

import com.DHH.Config.ApiConstant;
import com.DHH.Dto.Message;
import com.DHH.Entity.Base.BaseUser;
import com.DHH.Entity.Base.BaseUserInfo;
import com.DHH.Entity.Capital.CapitalSecurityGivebackRecord;
import com.DHH.Entity.Order.OrderInfo;
import com.DHH.Entity.Order.OrderInfoState;
import com.DHH.Entity.Order.OrderInfoStateRecord;
import com.DHH.Entity.WxOrder.WxOrderInfo;
import com.DHH.Mapper.Api.Order.ApiOrderInfoMapper;
import com.DHH.Mapper.Base.BaseUserInfoMapper;
import com.DHH.Mapper.Capital.CapitalSecurityGivebackRecordMapper;
import com.DHH.Mapper.Order.OrderInfoStateMapper;
import com.DHH.Mapper.Order.OrderInfoStateRecordMapper;
import com.DHH.Mapper.WxOrder.WxOrderInfoMapper;
import com.DHH.Util.OrderNumberGen;
import com.DHH.wx.action.Pay_Refund;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * Created by 汪玉东 on 2017/8/7.
 */
@Service
public class ApiOrderServicesImpl  implements  ApiOrderServices{
    @Resource
    private OrderInfoStateMapper orderInfoStateMapper;
    @Resource
    private ApiOrderInfoMapper apiOrderInfoMapper;
    @Resource
    private OrderInfoStateRecordMapper orderInfoStateRecordMapper;
    @Resource
    private BaseUserInfoMapper baseUserInfoMapper;
    @Resource
    private WxOrderInfoMapper wxOrderInfoMapper;
    @Resource
    private CapitalSecurityGivebackRecordMapper capitalSecurityGivebackRecordMapper;
    @Override
    public Message insert(String did, String longitude, String latitude, BaseUser user) {

        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setOrderNumber(OrderNumberGen.getOrderIdByUUId());
        orderInfo.setUserId(user.getId());
        orderInfo.setStartTime(new Date());
        Float m=new Float(0.00);
        orderInfo.setMoney(m);
        orderInfo.setStartTime(new Date());
        orderInfo.setDuration(0);
        orderInfo.setDeviceId(did);
        orderInfo.setDisabled(0);

        OrderInfoState orderInfoState=new OrderInfoState();
        orderInfoState.setMark("cjdd");
        orderInfoState=orderInfoStateMapper.selectOne(orderInfoState);
        orderInfo.setStateId(orderInfoState.getId());

        OrderInfoStateRecord orderInfoStateRecord=new OrderInfoStateRecord();




        apiOrderInfoMapper.insert(orderInfo);


        orderInfoStateRecord.setLat(latitude);
        orderInfoStateRecord.setLng(longitude);
        orderInfoStateRecord.setOrderId(orderInfo.getId());
        orderInfoStateRecord.setStateId(orderInfoState.getId());
        orderInfoStateRecord.setMsg("创建订单");
        orderInfoStateRecord.setCreateTime(new Date());
        orderInfoStateRecordMapper.insert(orderInfoStateRecord);


        Message message=new Message();
        if(orderInfo.getId()!=null){
            message.setState(true);
            message.setMsg("订单创建成功");

        }else{
            message.setState(false);
            message.setMsg("订单创建失败");
        }
        return  message;

  }

    @Override
    public Message updateOrderSettlement(OrderInfo orderInfo, String finshType) {

        OrderInfoState orderInfoState=new OrderInfoState();
        orderInfoState.setMark(finshType);
        orderInfoState=orderInfoStateMapper.selectOne(orderInfoState);

        orderInfo.setStateId(orderInfoState.getId());
        OrderInfoStateRecord orderInfoStateRecord=new OrderInfoStateRecord();
        orderInfo.setStateId(orderInfoState.getId());
        orderInfo.setEndTime(new Date());
        int mins =(int) Math.ceil ((orderInfo.getEndTime().getTime() - orderInfo.getStartTime().getTime())/(1000 * 60));
        if(mins<=0){
            mins=1;
        }
        orderInfo.setMoney((float) (Math.ceil(mins/60.0))* ApiConstant.WX_D_PRICE);
        apiOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);

        orderInfoStateRecord.setLat("0");
        orderInfoStateRecord.setLng("0");
        orderInfoStateRecord.setOrderId(orderInfo.getId());
        orderInfoStateRecord.setStateId(orderInfo.getId());
        orderInfoStateRecord.setMsg(orderInfoState.getSname());
        orderInfoStateRecord.setCreateTime(new Date());
        orderInfoStateRecordMapper.insert(orderInfoStateRecord);

        Message message=new Message();
        if(orderInfoStateRecord.getId()!=null){
            message.setState(true);
            message.setMsg("订单操作成功");

        }else{
            message.setState(false);
            message.setMsg("订单操作失败");
        }
        return  message;
    }

    @Override
    public Message updateOrderComplete(OrderInfo orderInfo) {
        Message message=new Message();
        OrderInfoState orderInfoState=new OrderInfoState();
        orderInfoState.setMark("ddwc");
        orderInfoState=orderInfoStateMapper.selectOne(orderInfoState);
        orderInfoState.getId();
        OrderInfoStateRecord orderInfoStateRecord=new OrderInfoStateRecord();
        orderInfoStateRecord.setMsg("订单完成");
        orderInfoStateRecord.setStateId(orderInfoState.getId());
        orderInfoStateRecord.setOrderId(orderInfo.getId());
        orderInfoStateRecord.setCreateTime(new Date());
        orderInfoStateRecord.setLng("0");
        orderInfoStateRecord.setLat("0");
        orderInfoStateRecordMapper.insert(orderInfoStateRecord);
        if(orderInfoStateRecord.getId()!=null){
            message.setState(true);
            message.setMsg("订单状态修改成功");
        }else{
            message.setState(false);
            message.setMsg("订单状态修改失败");
        }

        return  message;
    }

    @Override
    public Message updateOrderComplete(OrderInfo orderInfo, BaseUser user) {

        OrderInfoState orderInfoState=new OrderInfoState();
        orderInfoState.setMark("ddwc");
        orderInfoState=orderInfoStateMapper.selectOne(orderInfoState);

        Message message=new Message();
        if(orderInfo.getStateId()==orderInfoState.getId()){
            message.setState(false);
            message.setMsg("失败[订单已经结算]");
            return message;
        }


        BaseUserInfo baseUserInfo=new BaseUserInfo();
        baseUserInfo.setUserId(user.getId());
        baseUserInfo=baseUserInfoMapper.selectOne(baseUserInfo);
        Float money=baseUserInfo.getSurplusMoney();
        if(money<(orderInfo.getMoney()* ApiConstant.WX_MONEY_COM_TIMES)){
            message.setState(false);
            message.setMsg("失败[余额不足]");
            return message;
        }

        money=money-(orderInfo.getMoney()* ApiConstant.WX_MONEY_COM_TIMES);
        baseUserInfo.setSurplusMoney(money);
        baseUserInfoMapper.updateByPrimaryKey(baseUserInfo);


        orderInfo.setStateId(orderInfoState.getId());
        apiOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);

        OrderInfoStateRecord orderInfoStateRecord=new OrderInfoStateRecord();
        orderInfoStateRecord.setMsg("订单完成");
        orderInfoStateRecord.setStateId(orderInfoState.getId());
        orderInfoStateRecord.setOrderId(orderInfo.getId());
        orderInfoStateRecord.setCreateTime(new Date());
        orderInfoStateRecord.setLng("0");
        orderInfoStateRecord.setLat("0");
        orderInfoStateRecordMapper.insert(orderInfoStateRecord);
        if(orderInfoStateRecord.getId()!=null){
            message.setState(true);
            message.setMsg("订单状态修改成功");
        }else{
            message.setState(false);
            message.setMsg("订单状态修改失败");
        }

        return  message;
    }

    @Override
    public Message updateDepositComplete(CapitalSecurityGivebackRecord capitalSecurityGivebackRecord, BaseUser baseUser) {
        Message message=new Message();
        try{
            WxOrderInfo wxOrderInfo=new WxOrderInfo();
            wxOrderInfo.setUserId(capitalSecurityGivebackRecord.getUserId());
            wxOrderInfo.setOrderType(2);
            wxOrderInfo=wxOrderInfoMapper.selectByUserAndType(wxOrderInfo);
            Pay_Refund pay_refund=new Pay_Refund();
            Map map=pay_refund.postMap(wxOrderInfo.getWxorderId(),wxOrderInfo.getMoney(),wxOrderInfo.getMoney());
            System.out.println(map);
            if( map.get("result_code").equals("SUCCESS")){
                capitalSecurityGivebackRecord.setOperateId(baseUser.getId());
                capitalSecurityGivebackRecord.setOperateMsg("退还押金");
                capitalSecurityGivebackRecord.setOperateTime(new Date());
                capitalSecurityGivebackRecord.setStateId(2);
                capitalSecurityGivebackRecordMapper.updateByPrimaryKeySelective(capitalSecurityGivebackRecord);


                BaseUserInfo baseUserInfo=new BaseUserInfo();
                baseUserInfo.setUserId(capitalSecurityGivebackRecord.getUserId());
                baseUserInfo=baseUserInfoMapper.selectOne(baseUserInfo);
                baseUserInfo.setSecurityMoney((float)0.0);
                baseUserInfoMapper.updateByPrimaryKeySelective(baseUserInfo);


                message.setState(true);
            }else{
                message.setState(false);
                message.setMsg("退还押金失败");
            }

            return message;
        }catch (Exception e){
            message.setState(false);
            message.setMsg(e.getMessage());
            return message;
        }

    }

    @Override
    public Message updateOrderKeepOn(OrderInfo orderInfo) {
        Message message=new Message();

        OrderInfoState orderInfoState=new OrderInfoState();
        orderInfoState.setMark("cjdd");
        orderInfoState=orderInfoStateMapper.selectOne(orderInfoState);
        orderInfoState.getId();

        orderInfo.setStateId(orderInfoState.getId());
        apiOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);

        OrderInfoStateRecord orderInfoStateRecord=new OrderInfoStateRecord();
        orderInfoStateRecord.setMsg("继续骑行中");
        orderInfoStateRecord.setStateId(orderInfoState.getId());
        orderInfoStateRecord.setOrderId(orderInfo.getId());
        orderInfoStateRecord.setCreateTime(new Date());
        orderInfoStateRecord.setLng("0");
        orderInfoStateRecord.setLat("0");
        orderInfoStateRecordMapper.insert(orderInfoStateRecord);
        if(orderInfoStateRecord.getId()!=null){
            message.setState(true);
            message.setMsg("订单状态修改成功");
        }else{
            message.setState(false);
            message.setMsg("订单状态修改失败");
        }

        return  message;
    }


}
